package com.liit.controller.func;

import java.sql.Timestamp;

import javax.inject.Inject;

import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.liit.weixin.core.basic.accesstoken.AccessTokenSupport;
import cn.liit.weixin.qrcode.WeixinQrcode;

import com.liit.annotation.SystemLog;
import com.liit.controller.index.BaseController;
import com.liit.entity.MeetingFormMap;
import com.liit.entity.WeixinUserFormMap;
import com.liit.entity.WxuserGroupFormMap;
import com.liit.exception.SystemException;
import com.liit.mapper.MeetingMapper;
import com.liit.plugin.PageView;
import com.liit.util.Common;

/**
 * @author dingweibo
 * @Title: MeetingController.java
 * @Description: TODO(用一句话描述该文件做什么)
 * @date 2016年1月26日
 */
@Controller
@RequestMapping("/meeting/")
public class MeetingController extends BaseController {
	@Inject
	private MeetingMapper meetingMapper;

	@RequestMapping("list")
	public String listUI(Model model) throws Exception {
		model.addAttribute("res", findByRes());
		return Common.BACKGROUND_PATH + "/func/listMeeting";
	}

	@ResponseBody
	@RequestMapping("findByPage")
	public PageView findByPage(String pageNow, String pageSize) throws Exception {
		MeetingFormMap meetingFormMap = getFormMap(MeetingFormMap.class);
		meetingFormMap = toFormMap(meetingFormMap, pageNow, pageSize, meetingFormMap.getStr("orderby"));
		pageView.setRecords(meetingMapper.findByPage(meetingFormMap));
		return pageView;
	}

	@RequestMapping("addUI")
	public String addUI(Model model) throws Exception {
		return Common.BACKGROUND_PATH + "/func/addMeeting";
	}

	@ResponseBody
	@RequestMapping("addEntity")
	@SystemLog(module = "功能管理", methods = "会议系统-新增会议")
	// 凡需要处理业务逻辑的.都需要记录操作日志
	@Transactional(readOnly = false)
	// 需要事务操作必须加入此注解
	public String addEntity() {
		try {
			Timestamp now = new Timestamp(System.currentTimeMillis());
			MeetingFormMap meetingFormMap = getFormMap(MeetingFormMap.class);
			meetingFormMap.put("createTime", now);
			meetingFormMap.put("lastOpenTime", now);
			meetingMapper.addEntity(meetingFormMap);
			// 微信带参数二维码
			// meetingFormMap.put("imgPath", WeixinQrcode.genQR(600000,
			// meetingFormMap.getLong("id").toString(),
			// AccessTokenSupport.getAccess_token()));
			meetingFormMap.put("imgPath", WeixinQrcode.genLimitQR(meetingFormMap.getLong("id").toString(), null, AccessTokenSupport.getAccess_token()));
			meetingMapper.editEntity(meetingFormMap);
		} catch (Exception e) {
			throw new SystemException("新增会议异常");
		}
		return "success";
	}

	@RequestMapping("editUI")
	public String editUI(Model model) throws Exception {
		String id = getPara("id");
		if (Common.isNotEmpty(id)) {
			model.addAttribute("meeting", meetingMapper.findbyFrist("id", id, MeetingFormMap.class));
		}
		return Common.BACKGROUND_PATH + "/func/editMeeting";
	}

	@ResponseBody
	@RequestMapping("editEntity")
	@Transactional(readOnly = false)
	// 需要事务操作必须加入此注解
	@SystemLog(module = "功能管理", methods = "会议系统-修改会议")
	// 凡需要处理业务逻辑的.都需要记录操作日志
	public String editEntity() throws Exception {
		MeetingFormMap meetingFormMap = getFormMap(MeetingFormMap.class);
		meetingMapper.editEntity(meetingFormMap);
		return "success";
	}

	@ResponseBody
	@RequestMapping("updateStatus")
	@Transactional(readOnly = false)
	// 需要事务操作必须加入此注解
	@SystemLog(module = "功能管理", methods = "会议系统-开启/关闭会议")
	// 凡需要处理业务逻辑的.都需要记录操作日志
	public String updateStatus() throws Exception {
		MeetingFormMap meetingFormMap = getFormMap(MeetingFormMap.class);
		if ("0".equals(meetingFormMap.getStr("status"))) {
			meetingFormMap.put("lastOpenTime", new Timestamp(System.currentTimeMillis()));
		} else if ("1".equals(meetingFormMap.getStr("status"))) {
			meetingFormMap.put("lastCloseTime", new Timestamp(System.currentTimeMillis()));
		} else {
			logger.error("状态有误");
			return "";
		}
		meetingMapper.editEntity(meetingFormMap);
		return "success";
	}

	/**
	 * 查看会议
	 * 
	 * @param id
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("viewMeeting")
	public String viewMeeting(Integer id, Model model) throws Exception {

		model.addAttribute("meeting", meetingMapper.findbyFrist("id", id.toString(), MeetingFormMap.class));

		WeixinUserFormMap weixinUserFormMap = new WeixinUserFormMap();
		weixinUserFormMap.put("where", "where openid in (select openid from ly_meeting_wxuser where meetingId =" + id + ")");
		model.addAttribute("meetingWxuser", meetingMapper.findByWhere(weixinUserFormMap));
		weixinUserFormMap.put("where", "where openid in (select openid from ly_meeting_wxuser_his where meetingId =" + id + ")");
		model.addAttribute("meetingWxuserHis", meetingMapper.findByWhere(weixinUserFormMap));

		// 查询分组
		model.addAttribute("group", meetingMapper.findByWhere(new WxuserGroupFormMap()));

		return Common.BACKGROUND_PATH + "/func/viewMeetingWxuser";
	}
}
